package leetcode;

import leetcode.vo.ListNode;

import java.util.HashSet;

public class LinkedListCycle {

    public static void main(String[] args) {
        ListNode n1 = new ListNode();
        n1.val = 2;
        ListNode n2 = new ListNode();
        n2.val = 4;
        ListNode n3 = new ListNode();
        n3.val = 3;
        n1.next = n2;
        n2.next = n3;
        n3.next = n2;

        LinkedListCycle linkedListCycle = new LinkedListCycle();
        System.out.println(linkedListCycle.hasCycle(n1));
    }

    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        HashSet<ListNode> set = new HashSet<>();
        Integer i = 0;
        while (head.next != null){
            head = head.next;
            if(set.contains(head.next)) return true;
            set.add(head.next);

            i ++;
        }
        return false;
    }

}

